<template>
  <div>
    <!-- 返回仪表盘按钮 -->
    <el-button type="primary" @click="navigateToDashboard" icon="el-icon-arrow-left">返回仪表盘</el-button>
    <!-- 添加用户按钮 -->
    <el-button type="success" @click="showAddUserDialog" icon="el-icon-plus">添加用户</el-button>
    <!-- 用户名搜索框 -->
    <div style="opacity: 0.8;">
      <el-input v-model="searchQuery" placeholder="输入用户名查找" style="margin: 20px 0;"></el-input>
    </div>

    <!-- 添加用户对话框 -->
    <el-dialog title="添加用户" :visible.sync="addUserDialogVisible">
      <el-form :model="newUser" @submit.native.prevent="addUser" :rules="rules" ref="newUserForm">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="newUser.username"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="newUser.email"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="newUser.password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="addUser">提交</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

    <!-- 编辑用户对话框 -->
    <el-dialog title="编辑用户" :visible.sync="editUserDialogVisible" :style="{ backgroundColor: 'rgba(255, 255, 255, 0.8)' }">
      <el-form :model="editUserData" @submit.native.prevent="updateUser" :rules="rules" ref="editUserForm">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="editUserData.username"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="editUserData.email"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="updateUser">保存</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    
    <div style="opacity: 0.8;">
      <!-- 用户列表 -->
      <el-table :data="filteredUsers" style="margin-top: 20px;">
        <el-table-column prop="id" label="用户ID"></el-table-column>
        <el-table-column prop="username" label="用户名"></el-table-column>
        <el-table-column prop="email" label="邮箱"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <!-- 编辑按钮 -->
            <el-button size="mini" type="primary" @click="openEditUserDialog(scope.$index)">编辑</el-button>
            <!-- 删除按钮 -->
            <el-button size="mini" type="danger" @click="deleteUser(scope.$index)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      addUserDialogVisible: false, // 控制添加用户对话框显示状态
      editUserDialogVisible: false, // 控制编辑用户对话框显示状态
      newUser: { // 新用户数据
        username: '',
        email: '',
        password: ''
      },
      editUserData: { // 编辑的用户数据
        username: '',
        email: ''
      },
      editIndex: -1, // 编辑用户的索引
      searchQuery: '', // 搜索查询关键字
      rules: { // 表单验证规则
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' },
          { type: 'email', message: '请输入有效的邮箱', trigger: ['blur', 'change'] }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      }
    };
  },
  computed: {
    users() {
      return this.$store.state.users; // 获取用户列表数据
    },
    filteredUsers() {
      // 根据搜索查询过滤用户列表
      const query = this.searchQuery.trim().toLowerCase();
      return this.users.filter(user => 
        user.username.toLowerCase().includes(query)
      );
    }
  },
  mounted() {
    // 获取用户数据
    this.$store.dispatch('fetchUsers');
  },
  methods: {
    navigateToDashboard() {
      // 返回仪表盘页面
      this.$router.push({ name: 'dashboard' });
    },
    showAddUserDialog() {
      // 显示添加用户对话框
      this.addUserDialogVisible = true;
    },
    addUser() {
      // 表单验证并添加用户
      this.$refs.newUserForm.validate((valid) => {
        if (valid) {
          this.$store.dispatch('addUser', this.newUser);
          this.addUserDialogVisible = false;
          this.newUser = { // 清空表单数据
            username: '',
            email: '',
            password: ''
          };
        }
      });
    },
    openEditUserDialog(index) {
      // 打开编辑用户对话框
      this.editUserData = { ...this.users[index] };
      this.editIndex = index;
      this.editUserDialogVisible = true;
    },
    updateUser() {
      // 表单验证并更新用户
      this.$refs.editUserForm.validate((valid) => {
        if (valid) {
          this.$store.dispatch('updateUser', { index: this.editIndex, user: this.editUserData });
          this.editUserDialogVisible = false;
        }
      });
    },
    deleteUser(index) {
      // 删除用户
      this.$store.dispatch('deleteUser', index);
    }
  }
};
</script>
